/*
 * @lc app=leetcode.cn id=13 lang=typescript
 *
 * [13] 罗马数字转整数
 */

// @lc code=start
function romanToInt(s: string): number {
    let match = {
        I: 1,
        V: 5,
        X: 10,
        L: 50,
        C: 100,
        D: 500,
        M: 1000,
    };
    let big = 0;
    return s
        .split("")
        .reverse()
        .reduce((col, cur) => {
            const num = match[cur];
            if (num < big) {
                return col - num;
            }
            big = num;
            return col + num;
        }, 0);
}
// @lc code=end
